<?php
namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Lib\Response;
use App\Http\Model\Hint;
use App\Http\Services\EquipServices;
use App\Http\Services\UserServices;

class ApiEquipController extends BaseController
{
    protected $equipServices;
    protected $userServices;

    public function __construct()
    {
        $this->equipServices = new EquipServices();
        $this->userServices = new UserServices();
    }

    /**
     * 获取装备类型
     */
    public function getEquipType(Request $request)
    {
        $data = $this->equipServices->getEquipType();

        Response::returnResult(Hint::SUCCESS, $data);
    }

    /**
     * 获取用户正在使用的装备
     */
    public function getUserUseEquip(Request $request)
    {
        $userId = (int) $request->input("user_id");
        $data = $this->equipServices->getUserUseEquip($userId);

        Response::returnResult(Hint::SUCCESS, $data);
    }

    /**
     * 获取用户拥有的装备列表
     */
    public function getUserHasEquip(Request $request)
    {
        $userId = (int) $request->input("user_id");
        $equipType = (int) $request->input("equip_type");
        $page = (int) $request->input('page_index', 0);
        $limit = (int) $request->input('page_size', 10);
        $data = $this->equipServices->getUserHasEquipList($page, $limit, $userId, $equipType);

        Response::returnResult(Hint::SUCCESS, $data);
    }

    /**
     * 用户装备装备
     */
    public function userUseEquip(Request $request)
    {
        $userId = $this->getLoginUserId();
        if(empty($userId)) {
            Response::returnResult(Hint::LOGIN_OUT);
        }
        $equipIds = $request->input("equip_ids");
        $equipIds = json_decode($equipIds, true);
        if(!is_array($equipIds)) {
            Response::returnResult(Hint::PARAM_ERROR);
        }

        $data = $this->equipServices->userUseEquip($userId, $equipIds);
        if($data['code'] != Hint::SUCCESS) {
            Response::returnResult($data['code'], $data['data'], $data['msg']);
        }

        Response::returnResult(Hint::SUCCESS, $data['data']);
    }

    /**
     * 获取装备货架列表
     */
    public function getEquipShelveList(Request $request)
    {
        $userId = $this->getLoginUserId();
        if(empty($userId)) {
            Response::returnResult(Hint::LOGIN_OUT);
        }
        $equipType = (int) $request->input("equip_type");

        $data = $this->equipServices->getEquipShelveList($userId, $equipType);

        Response::returnResult(Hint::SUCCESS, $data);
    }

    /**
     * 购买货架装备
     */
    public function buyEquipShelve(Request $request)
    {
        $userId = $this->getLoginUserId();
        if(empty($userId)) {
            Response::returnResult(Hint::LOGIN_OUT);
        }
        $shelves = $request->input("shelves");
        $shelves = json_decode($shelves, true);
        if(!is_array($shelves) || empty($shelves)) {
            Response::returnResult(Hint::PARAM_ERROR);
        }
        $ifUse = $request->input("if_use", 0);

        $data = $this->equipServices->buyEquipShelve($userId, $shelves, $ifUse);
        if($data['code'] != Hint::SUCCESS) {
            Response::returnResult($data['code'], $data['data'], $data['msg']);
        }

        Response::returnResult(Hint::SUCCESS, $data['data']);
    }






}
